import tkinter
import time
import serial
# 打开串口
ser = serial.Serial('COM4', 9600, timeout=1)  # 设置串口名、波特率和超时时间

# 定义16进制数据，使用bytes.fromhex()方法将16进制字符串转换为字节
hex_data = '01 03 01 FF 00 0C 74 03'  # 示例16进制数据
data_to_send = bytes.fromhex(hex_data)
r = tkinter.Tk()
r.resizable(False,False)
r.config(bg="#90EE90")
r.geometry("1200x800")
r.title('传感器软件')
#头部布局
li = tkinter.Label(r,
    text="数据可视化情况",
    bg="#90EE90",
    font=('楷体', 25),
    fg='red'
    )
li.pack()
li.place(x=80,y=20)
#校徽
photo = tkinter.PhotoImage(file="img.png",)
liimg = tkinter.Label(r,
    image=photo,
    width=50,
    height=50,
    bg="#90EE90"
)
liimg.pack()
liimg.place(x=20,y=10)
#分割线
li1 = tkinter.Label(r)
li1.pack()
li1.place(x=10,y=70,height=3,width=1180)
#圆
cv1 = tkinter.Canvas(r,bg='#90EE90')
cv1.config(highlightthickness=0)
cv2 = tkinter.Canvas(r,bg='#90EE90')
cv2.config(highlightthickness=0)
cv3 = tkinter.Canvas(r,bg='#90EE90')
cv3.config(highlightthickness=0)
cv4 = tkinter.Canvas(r,bg='#90EE90')
cv4.config(highlightthickness=0)
cv1.place(width=280,height=500,x=0,y=150)
cv2.place(width=280,height=500,x=306.6,y=150)
cv3.place(width=280,height=500,x=613.2,y=150)
cv4.place(width=280,height=500,x=919.8,y=150)
s = 300
#第一个圆
cv1.create_arc(50, 70, 230, 250, extent=-359,fill="#fdd06d",start=90,outline='#98dede')
cv1a = cv1.create_arc(50, 70, 230, 250, extent=s,fill="#98FB98",start=90,outline='#98FB98')
cv1aa = cv1.create_arc(65, 85, 215, 235, extent=-359,fill="#dddddd",start=90,outline='#dddddd')
cv1aaa = cv1.create_oval(70, 90, 210, 230,outline='#90EE90',fill='#90EE90')
cv1_text=cv1.create_text(145,160,text="99％",font=("楷体",25),fill="white",)
cv1_text_a = cv1.create_text(145,420,text="温度：100°C",font=("楷体",22),fill="#1E90FF")
#第二个圆 #00FA9A
cv2.create_arc(50, 70, 230, 250, extent=-359,fill="#64eced",start=90,outline='#64eced')
cv2a = cv2.create_arc(50, 70, 230, 250, extent=200,fill="#98FB98",start=90,outline='#98FB98')
cv2aa = cv2.create_arc(65, 85, 215, 235, extent=-359,fill="#dddddd",start=90,outline='#dddddd')
cv2aaa = cv2.create_oval(70, 90, 210, 230,outline='#90EE90',fill='#90EE90')
cv2_text=cv2.create_text(145,160,text="99％",font=("楷体",25),fill="white",)
cv2_text_a = cv2.create_text(145,420,text="湿度：100g/m3",font=("楷体",22),fill="#1E90FF")
#第三个圆
cv3.create_arc(50, 70, 230, 250, extent=-359,fill="#fd93a9",start=90,outline='#fd93a9')
cv3.create_arc(50, 70, 230, 250, extent=250,fill="#98FB98",start=90,outline='#98FB98')
cv3.create_arc(65, 85, 215, 235, extent=-359,fill="#dddddd",start=90,outline='#dddddd')
cv3.create_oval(70, 90, 210, 230,outline='#90EE90',fill='#90EE90')
cv3_text=cv3.create_text(145,160,text="99％",font=("楷体",25),fill="white",)
cv3_text_a = cv3.create_text(145,420,text="环境光强度：100",font=("楷体",22),fill="#1E90FF")
#第四个圆
cv4.create_arc(50, 70, 230, 250, extent=-359,fill="#00FA9A",start=90,outline='#00FA9A')
cv4.create_arc(50, 70, 230, 250, extent=100,fill="#98FB98",start=90,outline='#98FB98')
cv4.create_arc(65, 85, 215, 235, extent=-359,fill="#dddddd",start=90,outline='#dddddd')
cv4.create_oval(70, 90, 210, 230,outline='#90EE90',fill='#90EE90')
cv4_text=cv4.create_text(145,160,text="99％",font=("楷体",25),fill="white",)
cv4_text_a = cv4.create_text(145,420,text="二氧化碳含量：100",font=("楷体",22),fill="#1E90FF")
def se():
    # 发送数据
    ser.write(data_to_send)
    # 接收数据（可选）
    data = ser.readline()  # 读取一行数据
    dataa = ['\\x{:02x}'.format(byte) for byte in data]
    print(dataa)  # 打印接收到的数据
    wd = str(int((dataa[5]+dataa[6]).replace('\\x',''),16) / 10)
    sd = str(int((dataa[7]+dataa[8]).replace('\\x',''),16) / 10)
    fun(wd,sd)
def fun(a,b):
    global cv1a,cv1aa,cv1aaa,s,cv1_text,cv1_text_a,cv2a,cv2aa,cv2aaa,cv2_text,cv2_text_a
    print(1)
    #设置温度最高为40℃，最低为-10℃如果超过这个区间进行报警
    s = 360 - int(float(a))*(360/50)
    #设置湿度最高为75%，最低为7.2%如果超过这个区间进行报警
    ss = 360 - int(float(b))*(360/100)
    if int(float(a))>500 and int(float(b)>500):
        return False
    cv1.delete(cv1a,cv1aa,cv1aaa,cv1_text,cv1_text_a)
    cv2.delete(cv2a,cv2aa,cv2aaa,cv2_text,cv2_text_a)
    cv1a = cv1.create_arc(50, 70, 230, 250, extent=s,fill="#98FB98",start=90,outline='#98FB98')
    cv1aa = cv1.create_arc(65, 85, 215, 235, extent=-359,fill="#dddddd",start=90,outline='#dddddd')
    cv1aaa = cv1.create_oval(70, 90, 210, 230,outline='#90EE90',fill='#90EE90')
    cv1_text= cv1.create_text(145,160,text=str(int(float(a))*2)+"％",font=("楷体",25),fill="white")
    cv1_text_a = cv1.create_text(145,420,text="温度："+a+"℃",font=("楷体",22),fill="#1E90FF")
    cv2a = cv2.create_arc(50, 70, 230, 250, extent=ss,fill="#98FB98",start=90,outline='#98FB98')
    cv2aa = cv2.create_arc(65, 85, 215, 235, extent=-359,fill="#dddddd",start=90,outline='#dddddd')
    cv2aaa = cv2.create_oval(70, 90, 210, 230,outline='#90EE90',fill='#90EE90')
    cv2_text=cv2.create_text(145,160,text=b+"％",font=("楷体",25),fill="white",)
    cv2_text_a = cv2.create_text(145,420,text="湿度："+b+"g/m3",font=("楷体",22),fill="#1E90FF")
def whileafter():
    se()
    r.after(500,whileafter)
whileafter()
r.mainloop()
ser.close()
